package com.nl.controller.xuyi.service;

import java.util.ArrayList;
import java.util.List;

import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.model.BaseResult;
import com.nl.controller.common.service.CommonService;
import com.nl.controller.web.global.GlobalConstants;
import com.nl.controller.xuyi.bean.MgrRelYwReq;
import com.nl.controller.xuyi.bean.pocbo.QryYunWeiListDispatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.nl.controller.web.bean.SmsMgrTemplateInfo;

import com.nl.controller.xuyi.mapper.XuYiMgrTemplateMapper;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

@Service
@Slf4j
public class XuYiMgrTemplateService {
	@Autowired
	XuYiMgrTemplateMapper xuYiMgrTemplateMapper;

	@Autowired
	private CommonService commonService;
	
	public List<SmsMgrTemplateInfo> findMgrTemplateByName(String mgrName) {
		
		List<SmsMgrTemplateInfo> smsMgrTemplateInfoList= null;
		try {
			smsMgrTemplateInfoList = xuYiMgrTemplateMapper.findMgrTemplateByName(mgrName);
			
		} catch (Exception e) {
			log.info("根据项目名查询项目详情出现异常:"+e.toString());
		}
		return smsMgrTemplateInfoList;
	}

	public SmsMgrTemplateInfo findMgrTemplateById(String mgrId) {
		SmsMgrTemplateInfo smsMgrTemplateInfo= null;
		try {
			smsMgrTemplateInfo = xuYiMgrTemplateMapper.findMgrTemplateById(mgrId);
			
		} catch (Exception e) {
			log.info("根据项目编码查询项目详情出现异常:"+e.toString());
		}
		return smsMgrTemplateInfo;
	}

	public int modifyContent(String mgrId, String content,String contentType) {
		int result = 0;
		try {
			result = xuYiMgrTemplateMapper.modifyContent(mgrId,content,contentType);
		} catch (Exception e) {
			log.info("变更项目发送内容发生异常:"+e.toString());
			e.printStackTrace();
		}
		return result;
	}

	public int configureMgrTemplate(String cityId, String countyId, String name, String extId, String startTime, String endTime, String isLocal, int threshold, String isThreshold, String mgrId, String regionId, String regionType, String regionTypeValue, String msisdn, String cycle, String cycleThreshold, String localDate, int checkNum, String mgrFlag) {
		int result = 0;
		/* if(null==countyId||"".equals(countyId)){
    	        	countyId = cityId;
    	        }*/
		SmsMgrTemplateInfo smsMgrTemplateInfo = new SmsMgrTemplateInfo();
		smsMgrTemplateInfo.setCityId(cityId);
		smsMgrTemplateInfo.setCountyId(countyId);
		smsMgrTemplateInfo.setName(name);
		smsMgrTemplateInfo.setCityExtId(cityId+extId);
		smsMgrTemplateInfo.setStartDate(startTime);
		smsMgrTemplateInfo.setEndDate(endTime);
		smsMgrTemplateInfo.setIsLocal(isLocal);
		smsMgrTemplateInfo.setThreshold(threshold);
		smsMgrTemplateInfo.setIsThreshold(isThreshold);
		smsMgrTemplateInfo.setId(mgrId);
		smsMgrTemplateInfo.setRegionId(regionId);
		smsMgrTemplateInfo.setRegionType(regionType);
		smsMgrTemplateInfo.setRegionTypeValue(regionTypeValue);
		smsMgrTemplateInfo.setMsisdn(msisdn);
		smsMgrTemplateInfo.setCycle(cycle);
		smsMgrTemplateInfo.setCycleThreshold(cycleThreshold);
		smsMgrTemplateInfo.setLocalDate(localDate);
		smsMgrTemplateInfo.setCheckNum(1+"");
		try {
			result = xuYiMgrTemplateMapper.modifyMgrTemplate(smsMgrTemplateInfo);
			commonService.createSmsOperLog("2", "修改项目模板信息表"+mgrId, "0", "项目维护管理");
		} catch (Exception e) {
			e.printStackTrace();
			commonService.createSmsOperLog("2", "修改项目模板信息表"+mgrId, "1", "项目维护管理");
			log.error("配置模板属性项目id： " + mgrId);
			throw new SmsException("配置模板属性项目id： " + mgrId);
		}
		return result;
	}

    public BaseResult addMgrYunWei(MgrRelYwReq mgrRelYwReq) {
		BaseResult result = BaseResult.newFailInstance();
		int insertCount = 0 ;
		String mgrId;

		List<String> uidsList = new ArrayList<>();
		try {
			mgrId = mgrRelYwReq.getMgrId();
			uidsList = mgrRelYwReq.getUidsList();
			if(!CollectionUtils.isEmpty(uidsList)){
				for(String uid : uidsList){
					int insertResult = xuYiMgrTemplateMapper.addMgrYunWei(mgrId,uid);
					if(insertResult>0){
						insertCount++;
					}
				}
				result.setRetCode(GlobalConstants.SUCCESS_CODE);
				result.setRetMsg("插入项目与运维人员关联关系表成功，数量为"+insertCount);
				result.setData(insertCount);
			}
		}catch (Exception e){
			result.setRetMsg("插入项目与运维人员关联关系表发生异常");
			log.info("插入项目与运维人员关联关系表发生异常，"+e.toString());
			e.printStackTrace();
		}
		return result;
    }

	public BaseResult<List<QryYunWeiListDispatcher>> findYWByMgrId(String mgrId) {
		BaseResult<List<QryYunWeiListDispatcher>> result = BaseResult.newFailInstance();
		List<QryYunWeiListDispatcher> yunweiList= new ArrayList<>();
		try {
			yunweiList = xuYiMgrTemplateMapper.findYWByMgrId(mgrId);
			result.setData(yunweiList);
			result.setRetCode(GlobalConstants.SUCCESS_CODE);
			result.setRetMsg("根据项目编码查询关联的运维人员列表成功");
		}catch (Exception e){
			result.setRetMsg("根据项目编码查询关联的运维人员列表发生异常");
			log.info("根据项目编码查询关联的运维人员列表发生异常，"+e.toString());
			e.printStackTrace();
		}
		return result;
	}
}
